package com.eastfair.pay.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.pay.dao.InvoiceMapper;
import com.eastfair.pay.dto.InvoiceDTO;
import com.eastfair.pay.entity.Invoice;
import com.eastfair.pay.entity.InvoiceApply;
import com.eastfair.pay.enums.InvoiceStateEnum;
import com.eastfair.pay.service.ContractService;
import com.eastfair.pay.service.InvoiceApplyService;
import com.eastfair.pay.service.InvoiceService;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.pay.vo.InvoiceVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 发票
 * </p>
 *
 * @author clm
 * @date 2022-06-08
 */
@Slf4j
@Service
public class InvoiceServiceImpl extends SuperServiceImpl<InvoiceMapper, Invoice> implements InvoiceService {

    @Resource
    private InvoiceApplyService invoiceApplyService;

    @Resource
    private ContractService contractService;

//    @Resource(name = "lbRestTemplate")
    private RestTemplate restTemplate;

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<Invoice> modelList) {
        modelList.stream().forEach(s -> {
            s.setIsDeleted(BusinessConstant.YES);
        });
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }

    /**
     * 基础数据
     * @param model
     * @return
     */
    @Override
    protected R<Boolean> handlerSave(Invoice model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        model.setTenantId(ContextUtil.getTenantId());
        //雪花ID
        return R.successDef();
    }

    /**
     * 保存发票详情信息
     * @param invoiceDTO
     * @return
     */
    @Override
    public  void saveInvoicecCnfirm(List<InvoiceDTO> invoiceDTO) {

        List<Invoice> invoiceList = ConvertUtil.convertList(invoiceDTO, Invoice.class);
        BigDecimal invoiceAmount = new BigDecimal(0);

        for (Invoice list : invoiceList){
            list.setApplicantId(ContextUtil.getUserId());
            list.setApplicantName(ContextUtil.getRealName());
            list.setInvoiceState(InvoiceStateEnum.INVOICE_SUCCESS.getCode());
            list.setProjectId(ContextUtil.getProjectId());
            list.setSubsystemId(ContextUtil.getSubSystemId());
            list.setTenantId(ContextUtil.getTenantId());

            invoiceAmount = invoiceAmount.add(list.getInvoiceAmount());
        }

        saveBatch(invoiceList);
        InvoiceApply invoiceApply = invoiceApplyService.getById(invoiceList.get(0).getInvoiceApplyId());
        invoiceApply.setInvoiceAmount(invoiceAmount);
        invoiceApply.setInvoiceState(InvoiceStateEnum.INVOICE_SUCCESS.getCode());
        invoiceApply.setApplicantName(ContextUtil.getRealName());
        invoiceApply.setApplicantId(ContextUtil.getUserId());
        log.info("invoiceApply++++12345:{}", ContextUtil.getRealName());
        log.info("invoiceApply++++12345:{}", ContextUtil.getUserId());
        log.info("invoiceApply++++12345:{}", JSONObject.toJSONString(invoiceApply));
        invoiceApplyService.updateById(invoiceApply);

//        Contract contract = contractService.getByBusinessId(invoiceApply.getBusinessId());
//        contract.setInvoicedAmount(invoiceAmount);
//        contractService.updateById(contract);

        //拼装回调参数
//        InvoiceCallbackDTO invoiceCallbackDTO = new InvoiceCallbackDTO();
//        invoiceCallbackDTO.setBusinessId(invoiceApply.getBusinessId());
//        invoiceCallbackDTO.setInvoicedAmount(invoiceAmount);
        //回调开票
//        restTemplate.postForEntity("http://" + invoiceApply.getCallbackServiceName() + invoiceApply.getCallbakcApi(), JSONObject.toJSON(invoiceCallbackDTO), R.class);

    }

    /**
     * 通过发票申请ID查询发票记录
     * @param invoiceDTO
     * @return
     */
    @Override
    public List<InvoiceVO> queryInvoicecList(InvoiceDTO invoiceDTO) {

        QueryWrapper<Invoice> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Invoice::getInvoiceApplyId,invoiceDTO.getInvoiceApplyId()).orderByDesc(Invoice::getCreateTime);
        List<Invoice> invoiceList = list(queryWrapper);
        List<InvoiceVO> invoiceVOList = ConvertUtil.convertList(invoiceList, InvoiceVO.class);
        return invoiceVOList;
    }
}
